<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            width: 100px;
            height: 100px;
            background-color: springgreen;
            position: absolute;
            top: 100px;
            left: 200px;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: rgb(255, 123, 0);
            position: absolute;
            top: 200px;
            left: 100px;
        }
    </style>
</head>
<body>
    <div></div>
    <p></p>
    <!-- 对多个盒子移动进行封装 -->
    <script>
        const box = document.querySelector("div")
        const p = document.querySelector("p")
        /* 点击box盒子 */
        box.onclick = function(){
            //调用黑子的移动函数
            //参数一 当前盒子在移动
            //参数二 移动的方向
            //蚕食三 移动最终的位置
            move(this,"left",400)
        }
        /* 点击p标签 */
        p.onclick = function(){
            move(this,"top",600)
        }
        /* 如何封装 */
        function move(ele,type,target){
            // console.log(ele,type,target);
            //第一步: 获取盒子的初始位置
            let current = parseInt(window.getComputedStyle(ele)[type])
            // console.log(current);
            let timer = setInterval(() => {
                //定时器每循环一次要+=5
                current += 5;
                //赋值
                ele.style[type] = current+"px"
                if(current === target){
                    clearInterval(timer)
                    //释放一下空间
                    timer = null;
                }
            }, 16);
        }
    </script>

</body>
</html>